<template>
	<view class="wrap">
		<u-form :model="model" :rules="rules" ref="uForm" :errorType="errorType">
			<u-form-item :label-position="labelPosition" :label-width="150" label="开户公司" prop="companyName">
				<u-input :border="border" v-model="model.companyName" @click="showCompanySelect = true" type="select" disabled placeholder="请选择期货公司" />
				<u-select v-model="showCompanySelect" title="选择期货公司" mode="single-column" @confirm="companySelectCallback" :list="companyList" />
			</u-form-item>
			<u-form-item :label-position="labelPosition" :label-width="150" label="资金账号" prop="UserID">
				<u-input :border="border" type="number" v-model="model.UserID" placeholder="请输入资金账号" />
			</u-form-item>
			<u-form-item :label-position="labelPosition" :label-width="150" label="旧密码" prop="OldPassword">
				<u-input :password-icon="true" :border="border" type="password" v-model="model.OldPassword" placeholder="请输入旧密码" />
			</u-form-item>
			<u-form-item :label-position="labelPosition" :label-width="150" label="新密码" prop="NewPassword">
				<u-input :password-icon="true" :border="border" type="password" v-model="model.NewPassword" placeholder="请输入密码" />
			</u-form-item>
			<u-form-item :label-position="labelPosition" :label-width="150" label="确认密码" label-width="150" prop="rePassword">
				<u-input :border="border" type="password" v-model="model.rePassword" placeholder="请确认密码" />
			</u-form-item>
		</u-form>
		<u-button @click="submit" type="primary">提交</u-button>
	</view>
</template>

<script>
	var dcRichAlert = uni.requireNativePlugin('Trader')
	import {
		getStrategyCompanyList
	} from '@/api/member.js'
	export default {
		data() {
			let that = this;
			return {
				model: {},
				rules: {
					companyName: [{
						required: true,
						message: '请选择期货公司',
						trigger: ['change', 'blur']
					}],
					UserID: [{
						required: true,
						message: '请输入账号',
						trigger: ['change', 'blur']
					}],
					OldPassword: [{
							required: true,
							message: '请输入密码',
							trigger: ['change', 'blur']
						},
						{
							// 正则不能含有两边的引号
							pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]+\S{7,12}$/,
							message: '需同时含有字母和数字，长度在8-12之间',
							trigger: ['change', 'blur']
						}
					],
					NewPassword: [{
							required: true,
							message: '请输入密码',
							trigger: ['change', 'blur']
						},
						{
							// 正则不能含有两边的引号
							pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]+\S{7,12}$/,
							message: '需同时含有字母和数字，长度在8-12之间',
							trigger: ['change', 'blur'],
						}
					],
					rePassword: [{
							required: true,
							message: '请重新输入密码',
							trigger: ['change', 'blur']
						},
						{
							validator: (rule, value, callback) => {
								return value === this.model.NewPassword;
							},
							message: '两次输入的密码不相等',
							trigger: ['change', 'blur']
						}
					],
				},
				border: false,
				check: false,
				companyList: [],
				showCompanySelect: false,
				labelPosition: 'left',
				errorType: ['message']
			};
		},
		onLoad() {
			this.getCompanyList()
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			// 查询期货公司列表
			getCompanyList: function() {
				getStrategyCompanyList().then(res => {
					if(res.code === 200) {
						this.companyList = res.data.map(item => {
							return {
								label: item.name,
								value: item.id
							}
						})
					}
				}).catch((err) => {
					uni.showToast({
						title: err.message,
						icon: 'none'
					})
				})
			},
			// 选择期货公司
			companySelectCallback: function(data) {
				this.model.companyName = data[0].label
				this.model.BrokerID = Number(data[0].value)
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						// 验证通过
						let data = {}
						let formData = this.model
						Object.keys(formData).map((key)=> {
							data[key] = formData[key]
						})
						delete data.rePassword
						delete data.companyName
						this.loading = true
						try {
							dcRichAlert.ReqUserPasswordUpdate(data, function(res) {
								uni.showModal({
									title: '请求',
									content: JSON.stringify(res)
								})
								uni.hideLoading();
								if (res.code === 200) {
									uni.showToast({
										title: '重置密码成功,请重新登录',
										icon: 'none',
										duration: 1500
									})
									setTimeout(() => {
										uni.redirectTo({
											url: '/pages/user/dealLogin/dealLogin'
										});
									}, 1500)
								} else {
									uni.showToast({
										title: res.message,
										icon: 'none',
										duration: 1500
									})
								}
							})
						} catch(err) {
							uni.showToast({
								title: err.message,
								icon: 'none',
								duration: 1500
							})
						}
					} else {
						// 验证失败
					}
				});
			}
		}
	};
</script>

<style scoped lang="scss">
	.wrap {
		padding: 30rpx;
	}
</style>
